'''------------------------------------------------------------------------------------------------
 $ @Author: 杨尚其
 $ @Date: 2021-12-20 23:14:13
 $ @LastEditTime: 2021-12-21 11:16:39
 $ @LastEditors: Please set LastEditors
 $ @Description: 画精度图和成功率图
 $ @FilePath: \YSQTrackerEvaluationToolkit\TOOLS\draw.py
--------------------------------------------------------------------------------------------------'''
import sys
sys.path.append(r'./')
import numpy as np
import matplotlib.pyplot as plt
from Tools.CalculateCurveIntegral import calculateCurveIntegral as cci

'''-------------------------------------------------------------------------------------------
 * @description: 画精度图和成功率图,#*图像的显示和保存需要在函数外部进行
                ->要求: 当target == 'SUCC' 时 array.len = 100
                ->->要求: 当target == 'ACC' 时 array.len = 50
 * @param {*} target-> 'SUCC' he 'ACC'
 * @param {*} testSet-> 测试集的名称
 * @param {*} CalculatingIndexMethod->可以选择 'Threshold' 和 'Integral'
 * @param {*} array->np.array
 * @param {str} tracker->跟踪器名称 str
 * @return {*}
---------------------------------------------------------------------------------------------'''
def draw(target,testSet,array,tracker:str,CalculatingIndexMethod = None,videoName = None):
    if CalculatingIndexMethod != None:
        plt.figure(target+'-'+testSet+"-"+CalculatingIndexMethod)
        plt.title(target+'-'+testSet+"-"+CalculatingIndexMethod)
    elif videoName != None:
        plt.figure(target+'-'+testSet+"-"+videoName)
        plt.title(target+'-'+testSet+"-"+videoName)
    else:
        print('TOOLS\draw.py :'+ 'draw() :'+'中画图模式不匹配')
        return -1
    performance = -2

    if target == 'ACC' and CalculatingIndexMethod == 'Threshold':
        x = np.linspace(0,50,50)
        performance = array[20]
        performance = round(performance,3)    # 取小数点后三位
        plt.plot(x,array,label = tracker+' ['+str(performance)+']')
        plt.xlabel('Location error threshold')
        plt.ylabel('Precision')
        return performance

    elif target == 'ACC' and CalculatingIndexMethod == 'Integral':
        x = np.linspace(0,50,50)
        performance = cci(array)
        performance = round(performance,3)    # 取小数点后三位
        plt.plot(x,array,label = tracker+' ['+str(performance)+']')
        plt.xlabel('Location error threshold')
        plt.ylabel('Precision')
        return performance

    elif target == 'SUCC' and CalculatingIndexMethod == 'Threshold':
        performance = array[50]
        performance = round(performance,3)    # 取小数点后三位
        x = np.linspace(0.0,1.0,99)
        plt.plot(x,array[1:],label = tracker+' ['+str(performance)+']')
        plt.xlabel('Overlap threshold')
        plt.ylabel('Success rate')
        return performance

    elif target == 'SUCC' and CalculatingIndexMethod == 'Integral':
        performance = cci(array)
        performance = round(performance,3)    # 取小数点后三位
        x = np.linspace(0.0,1.0,99)
        plt.plot(x,array[1:],label = tracker+' ['+str(performance)+']')
        plt.xlabel('Overlap threshold')
        plt.ylabel('Success rate')
        return performance

    elif target == "ERR":
        x = np.array(list(range(array.shape[0])))
        plt.plot(x,array,label = tracker+': ERR')
    elif target == "IOU":
        x = np.array(list(range(array.shape[0])))
        plt.plot(x,array,label = tracker+': IOU')

    else:
        print('TOOLS\draw.py :'+ 'draw() :'+'中画图模式不匹配')
        return -1

if __name__ == '__main__':
    acc1 = np.random.randn(50)
    acc2 = np.random.randn(50)
    acc3 = np.random.randn(50)
    ACC = [acc1,acc2,acc3]

    succ1 = np.random.randn(100)
    succ2 = np.random.randn(100)
    succ3 = np.random.randn(100)
    SUCC = [succ1,succ2,succ3]

    i = 0
    for acc in ACC:
        i+=1
        draw('ACC','OTB','Integral',acc,str(i))
    #! 在外部显示与保存图像
    plt.legend()
    plt.savefig('./OUTPUT/testFuncDrawAcc.jpg')

    i = 0
    for succ in SUCC:
        i+=1
        draw('SUCC','OTB','Integral',succ,str(i))
    #! 在外部显示与保存图像
    plt.legend()
    plt.savefig('./OUTPUT/testFuncDrawSucc.jpg')

    #! 在外部显示与保存图像
    plt.show()